(function (vc) {
    vc.extends({
        data: {
            addOwnerMemberInfo: {
                name: '',
                age: '',
                link: '',
                address: '',
                sex: '0',
                ownerTypeCd: '',
                remark: '',
                ownerId: '',
                ownerPhoto: '',
                ownerPhotoUrl: '',
                idCard: '',
                personType: 'P',
                personRole: '3',
                concactPerson: '',
                concactLink: '',
                attrs: []
            }
        },
        _initMethod: function () {
        },
        _initEvent: function () {
            vc.on('addOwnerMember', 'openAddOwnerModal', function (_owner) {
                $that.addOwnerMemberInfo.ownerId = _owner.ownerId;
                $('#addOwnerMemberModel').modal('show');
                $that._loadOwnerAttrSpec();
            });
        },
        methods: {
            addOwnerMemberValidate: function () {
                return vc.validate.validate({
                    addOwnerMemberInfo: $that.addOwnerMemberInfo
                }, {
                    'addOwnerMemberInfo.name': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "姓名不能为空"
                        },
                        {
                            limit: "maxin",
                            param: "2,64",
                            errInfo: "姓名长度必须在2位至64位"
                        }
                    ],
                    'addOwnerMemberInfo.link': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "手机号不能为空"
                        },
                        {
                            limit: "phone",
                            param: "",
                            errInfo: "手机号格式错误"
                        }
                    ],
                    'addOwnerMemberInfo.idCard': [
                        {
                            limit: "maxLength",
                            param: "64",
                            errInfo: "身份证格式不对"
                        }
                    ],
                    'addOwnerMemberInfo.sex': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "性别不能为空"
                        }
                    ],
                    'addOwnerMemberInfo.remark': [
                        {
                            limit: "maxLength",
                            param: "200",
                            errInfo: "备注长度不能超过200位"
                        }
                    ]
                });
            },
            saveOwnerMemberInfo: function () {
                if (!$that.addOwnerMemberValidate()) {
                    vc.toast(vc.validate.errInfo);
                    return;
                }


                $that.addOwnerMemberInfo.communityId = vc.getCurrentCommunity().communityId;
                vc.http.apiPost("/owner.saveOwnerMember",
                    JSON.stringify($that.addOwnerMemberInfo), {
                    emulateJSON: true
                },
                    function (json, res) {
                        //vm.menus = vm.refreshMenuActive(JSON.parse(json),0);
                        let _json = JSON.parse(json);
                        if (_json.code == 0) {
                            //关闭model
                            $('#addOwnerMemberModel').modal('hide');
                            //$that.clearAddOwnerInfo();
                            vc.toast("添加成功");
                            vc.emit('ownerDetailMember', 'listOwnerData', $that.addOwnerMemberInfo);
                            vc.emit('simplifyOwnerMember', 'listOwnerData', $that.addOwnerMemberInfo);

                            return;
                        } else {
                            vc.toast(_json.msg);
                        }
                    },
                    function (errInfo, error) {
                        console.log('请求失败处理');
                        vc.toast(errInfo);
                    });
            },
            clearAddOwnerInfo: function () {
                $that.addOwnerMemberInfo = {
                    name: '',
                    age: '',
                    link: '',
                    address: '',
                    sex: '0',
                    ownerTypeCd: '',
                    remark: '',
                    ownerId: '',
                    ownerPhoto: '',
                    ownerPhotoUrl: '',
                    idCard: '',
                    videoPlaying: true,
                    mediaStreamTrack: null,
                    attrs: []
                };
            },
            _uploadPhoto: function (event) {
                //$that.addOwnerMemberInfo.ownerPhoto = "";
                $("#uploadOwnerPhoto").trigger("click")
            },
            _choosePhoto: function (event) {
                let photoFiles = event.target.files;
                if (photoFiles && photoFiles.length > 0) {
                    // 获取目前上传的文件
                    let file = photoFiles[0]; // 文件大小校验的动作
                    if (file.size > 1024 * 1024 * 1) {
                        vc.toast("图片大小不能超过 1MB!")
                        return false;
                    }
                    // 改为异步上传图片
                    $that._doUploadImageAddOwner(file);
                }
            },
            // 异步上传图片
            _doUploadImageAddOwner: function (_file) {
                let param = new FormData();
                param.append("uploadFile", _file);
                param.append('communityId', vc.getCurrentCommunity().communityId);
                //发送get请求
                vc.http.upload('uploadFile',
                    'uploadImage',
                    param, {
                    emulateJSON: true,
                    //添加请求头
                    headers: {
                        "Content-Type": "multipart/form-data"
                    }
                },
                    function (json, res) {
                        let data = JSON.parse(json);
                        console.log(data);
                        $that.addOwnerMemberInfo.ownerPhoto = data.fileId;
                        $that.addOwnerMemberInfo.ownerPhotoUrl = data.url;
                    },
                    function (errInfo, error) {
                        console.log('请求失败处理');
                        vc.toast(errInfo);
                    }
                );
            },
            _loadOwnerAttrSpec: function () {
                $that.addOwnerMemberInfo.attrs = [];
                vc.getAttrSpec('building_owner_attr', function (data) {
                    data.forEach(item => {
                        item.value = '';
                        if (item.specShow == 'Y') {
                            item.values = [];
                            $that._loadAttrValue(item.specCd, item.values);
                            $that.addOwnerMemberInfo.attrs.push(item);
                        }
                    });
                });
            },
            _loadAttrValue: function (_specCd, _values) {
                vc.getAttrValue(_specCd, function (data) {
                    data.forEach(item => {
                        if (item.valueShow == 'Y') {
                            _values.push(item);
                        }
                    });
                });
            },
            _closeSaveOwnerModal: function () {
                $('#addOwnerMemberModel').modal('hide');
            },

        }
    });
})(window.vc);